package com.vonchenchen.usbmuxd_android.usbmuxd.muxdprotocol.protocol;

/**
 *
 * Created by lidechen on 2/25/17.
 */

public class UsbmuxdProtocol {

    public static final int VERSION = 1;
    public static final int USB_MTU = (3 * 16384);

/**
 enum mux_protocol {
 MUX_PROTO_VERSION = 0,
 MUX_PROTO_CONTROL = 1,
 MUX_PROTO_SETUP = 2,
 MUX_PROTO_TCP = IPPROTO_TCP,
 };
 */
    public static final int MUX_PROTO_VERSION = 0;
    public static final int MUX_PROTO_CONTROL = 1;
    public static final int MUX_PROTO_SETUP = 2;
    public static final int MUX_PROTO_ANDROIDINIT = 0X10;
    public static final int MUX_PROTO_TCP = TcpProtocol.IPPROTO_TCP;

 /**
    enum mux_dev_state {
        MUXDEV_INIT,	// sent version packet
        MUXDEV_ACTIVE,	// received version packet, active
        MUXDEV_DEAD		// dead
    };
  */
    public static final int MUXDEV_INIT = 0;
    public static final int MUXDEV_ACTIVE = 1;
    public static final int MUXDEV_DEAD = 2;

  /**
    enum mux_conn_state {
        CONN_IDLE,
        CONN_CONNECTING,
        CONN_CONNECTED
    };
   */
    public static final int CONN_IDLE = 0;
    public static final int CONN_CONNECTING = 1;
    public static final int CONN_CONNECTED = 2;

    /** hartbeat连接状态 */
    public static final int TCP_DISCONNECTED = 0;
    public static final int TCP_CONNECTED = 1;

    /**
     struct mux_header
     {
     uint32_t protocol;
     uint32_t length;
     };
     */
    public static class MuxHeader{
        public int protocol;
        public int length;

        public static final int size = 2*4;
    }

    /**
     struct version_header
     {
     uint32_t major;
     uint32_t minor;
     uint32_t padding;
     };
     */
    public static class VersionHeader{
        public int major;
        public int minor;
        public int padding;

        public static final int size = 3*4;
    }

    /**
     struct mux_connection
     {
     //	struct mux_device *dev;
     //	struct mux_client *client;
     SOCKET app_sockfd;
     enum mux_conn_state state;
     uint16_t sport, dport;
     uint32_t tx_seq, tx_ack, tx_sent, tx_win;
     uint32_t rx_seq, rx_recvd, rx_ack, rx_win;
     uint32_t max_payload;
     //uint32_t sendable;
     int flags;
     //	unsigned char *ib_buf;
     //	uint32_t ib_size;
     //	uint32_t ib_capacity;
     //	unsigned char *ob_buf;
     //	uint32_t ob_capacity;
     //	short events;
     uint64_t last_ack_time;
     };
     */
    public static class MuxConnection{

        public int state;
        public int sport;
        public int dport;

        public int tx_seq;
        public int tx_ack;
        public int tx_sent;
        public int tx_win;

        public int rx_seq;
        public int rx_recvd;
        public int rx_ack;
        public int rx_win;

        public int max_payload;

        public int flags;

        public long last_ack_time;

        public static final int size = 2*2 + 11*4 + 1*8;

        public void reset(){
            state = CONN_IDLE;
            sport = 0;
            dport = 0;

            tx_seq = 0;
            tx_ack = 0;
            tx_sent = 0;
            tx_win = 0;

            rx_seq = 0;
            rx_recvd = 0;
            rx_ack = 0;
            rx_win = 0;

            max_payload = 0;

            flags = 0;

            last_ack_time = 0;
        }
    }

//    #define	TH_FIN	0x01
//            #define	TH_SYN	0x02
//            #define	TH_RST	0x04
//            #define	TH_PUSH	0x08
//            #define	TH_ACK	0x10
//            #define	TH_URG	0x20
//            #define	TH_ECE	0x40
//            #define	TH_CWR	0x80
//            #define	TH_FLAGS	(TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)

    public static final int TH_FIN = 0x01;
    public static final int TH_SYN = 0x02;
    public static final int TH_RST = 0x04;
    public static final int TH_PUSH = 0x08;
    public static final int TH_ACK = 0x10;
    public static final int TH_URG = 0x20;
    public static final int TH_ECE = 0x40;
    public static final int TH_CWR = 0x80;
    public static final int TH_FLAGS = (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR);

    public static class TcpHeader{

        public short th_sport;
        public short th_dport;

        public int th_seq;
        public int th_ack;

        public char th_x2off;  // tx_x:4   th_off:4
        public char th_flags;

        public short th_win;
        public short th_sum;
        public short th_urp;

        public static final int size = 2*2 + 4*2 + 1*2 + 3*2;
    }
}













